<template>
	<view class="recharge">
		<view class="top">
			<text>USDT</text>
		</view>
		<view class="" v-if="inviteshow">
			<tki-qrcode ref="qrcode" unit='px' background='#fff' foreground='#000' :val="val" :loadMake='loadMake' :size="size" />
		</view>
		<view style="margin-bottom: 10%;margin-top: 6%;font-size: 14px;">{{address}}</view>
		<view class="btn">
			<button type="primary" style="background-color: #03AD8F;" @click="downloadImage">保存二维码</button>
		</view>
		<view class="btn" style="margin-bottom: 16%;">
			<button type="primary" @click="setClipboard">复制地址</button>
		</view>
	</view>
</template>

<script>
	import tkiQrcode from "@/components/tki-qrcode/tki-qrcode.vue"

	export default {
		data() {
			return {
				val: 'http://www.baidu.com.wwwwwww',
				size: 200,
				loadMake: true,
				inviteshow: false,
				address: ''
			};
		},
		components: {
			tkiQrcode
		},
		onLoad: function() {
			this.getData();
		},
		methods: {
			setClipboard: function() {
				var data = this.address;
				if (data.length === 0) {
					uni.showModal({
						title: '设置剪贴板失败',
						content: '内容不能为空',
						showCancel: false
					})
				} else {
					uni.setClipboardData({
						data: data,
						success: () => {
							uni.showToast({
								title: "复制成功",
								icon: "none",
								duration: 2000
							});
						},
						fail: () => {
							uni.showModal({
								content: '储存数据失败!',
								showCancel: false
							})
						}
					});
				}
			},
			getData: function() {
				const _this = this;
				//获取用户充值地址
				const data = {
					cid: '6'
				}
				this.$request.post("/api/wallet/getAddress", {
						data: data
					})
					.then(res => {
						if (res.data.code == 1) {
							_this.val = res.data.data;
							_this.address = res.data.data;
							_this.inviteshow = true;
						}
					})
					.catch(error => {
						console.error('error:', error);
					});
			},
			downloadImage: function() {
				//this.$refs.qrcode.result;获取二维码图片base64位地址,不能保存在变量中.
				var self = this
				let imgurl = self.$refs.qrcode.result;
				uni.showLoading({
					title: '下载中'
				})

				uni.downloadFile({
					url: imgurl,
					success: (res) => {
						uni.hideLoading();
						uni.saveImageToPhotosAlbum({
							filePath: imgurl, //图片路径
							success: function() {
								// alert('save success');
								uni.showToast({
									title: "保存成功",
									icon: "none",
									duration: 2000
								});
							}
						});
					},
					fail: (err) => {
						alert('downloadFile fail, err is:', err)
					}
				})
			}
		}

	}
</script>

<style lang="less">
	.recharge {
		width: 100%;
		display: flex;
		flex-direction: column;
		border-top: 5px solid #F1F1F1;
		align-items: center;

		.top {
			height: 100px;
			line-height: 100px;
		}

		.btn {

			width: 70%;
			margin: 0 auto 30px;
		}
	}
</style>
